package an.qt.hello;

import com.hiflying.smartlink.ISmartLinker;
import com.hiflying.smartlink.OnSmartLinkListener;
import com.hiflying.smartlink.SmartLinkedModule;
import com.hiflying.smartlink.v7.MulticastSmartLinker;

import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.os.Handler;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.widget.Toast;

import android.os.Bundle;

import an.qt.hello.NativeFunction;
import an.qt.hello.JHello;
public class SmartLinkForQt extends org.qtproject.qt5.android.bindings.QtActivity implements OnSmartLinkListener{

	public static final String EXTRA_SMARTLINK_VERSION = "EXTRA_SMARTLINK_VERSION";

	//	我的领域始
	protected static ISmartLinker mSnifferSmartLinker=null;//应该是源吧
	private static SmartLinkForQt root=null;
	private static boolean mIsConncting = false;
	protected static Handler mViewHandler = new Handler();
	protected ProgressDialog mWaitingDialog;
	private BroadcastReceiver mWifiChangedReceiver;
	public SmartLinkForQt () {
		installRoot();
		installSmartLinker();
	}
        public void onCreate(Bundle savedInstanceState) {
                // TODO Auto-generated method stub
                super.onCreate(savedInstanceState);
                //不能放在构造函数中
                mWifiChangedReceiver = new BroadcastReceiver() {

                        @Override
                        public void onReceive(Context context, Intent intent) {
                                ConnectivityManager connectivityManager = (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);
                                NetworkInfo networkInfo = connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
                                if (networkInfo != null && networkInfo.isConnected()) {
                                        //有链接，可以获得getssid了
                                        NativeFunction.javaSignal(1);
                                }else {
                                    NativeFunction.javaSignal(2);
                                        //链接断了，处理一下吧
                                }
                        }
                };
                registerReceiver(mWifiChangedReceiver, new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION));
        }
	public void installSmartLinker(){
		if(mSnifferSmartLinker==null){
			mSnifferSmartLinker=MulticastSmartLinker.getInstance();
		}
	}
	public void installRoot() {
		if(root==null){
			root=this ;
		}
	}

        private static String getSSID(){
            JHello.consoleblala("native方法main中");
               NativeFunction.hiQt();
               NativeFunction.hiQt2();
               int ggff=1;
               NativeFunction.javaSignal(ggff);
               NativeFunction.javaSignal(2);
		WifiManager wm = (WifiManager)root.getSystemService(WIFI_SERVICE);
		if(wm != null){
			WifiInfo wi = wm.getConnectionInfo();
			if(wi != null){
				String ssid = wi.getSSID();
				if(ssid.length()>2 && ssid.startsWith("\"") && ssid.endsWith("\"")){
					return ssid.substring(1,ssid.length()-1);
				}else{
					return ssid;
				}
			}
		}

		return "";
	}
	
        public static boolean startSmartLink(String ssid,String pwd) {
		if(!mIsConncting){

			//设置要配置的ssid 和pswd
			try {
				mSnifferSmartLinker.setOnSmartLinkListener(root);//设置监听器
				//开始 smartLink
				//getApplicationContext()返回单一上下文环境，全局程序的进程
				//						trim()估计是去除结尾开头的空格换行还有标点符号之类的
				mSnifferSmartLinker.start(root.getApplicationContext(), ssid.trim(), pwd.trim());
				//						设置配置标志位为true
				mIsConncting = true;
				//						开始转圈圈
//				mWaitingDialog.show();
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
                return mIsConncting;
	}
	
	
	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
		mSnifferSmartLinker.setOnSmartLinkListener(null);
		try {
			unregisterReceiver(mWifiChangedReceiver);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	//3个监听函数
	@Override//有东西连上了
	public void onLinked(final SmartLinkedModule module) {
		// TODO Auto-generated method stub
                NativeFunction.javaSignal(3);
//		Log.w(TAG, "onLinked");
		mViewHandler.post(new Runnable() {

			@Override
                        public void run() {//getString(R.string.hiflying_smartlinker_new_module_found, module.getMac(), module.getModuleIP())
                                Toast.makeText(getApplicationContext(),"==" ,
						Toast.LENGTH_SHORT).show();
			}
		});
	}


	@Override
	public void onCompleted() {
NativeFunction.javaSignal(4);
//		Log.w(TAG, "onCompleted");
		mViewHandler.post(new Runnable() {

			@Override
			public void run() {
				// TODO Auto-generated method stub
                                Toast.makeText(getApplicationContext(), "==2",
						Toast.LENGTH_SHORT).show();
//				mWaitingDialog.dismiss();
				mIsConncting = false;
			}
		});
	}


	@Override
	public void onTimeOut() {
NativeFunction.javaSignal(5);
//		Log.w(TAG, "onTimeOut");
		mViewHandler.post(new Runnable() {

			@Override
			public void run() {
				// TODO Auto-generated method stub
                                Toast.makeText(getApplicationContext(), "==3",
						Toast.LENGTH_SHORT).show();
//				mWaitingDialog.dismiss();
				mIsConncting = false;
			}
		});
	}
	
	public static void showAndroidInfo(final String info) {
		mViewHandler.post(new Runnable() {

			@Override
			public void run() {
                            NativeFunction.javaSignal(6);
				// TODO Auto-generated method stub
				Toast.makeText(root.getApplicationContext(),info, 
						Toast.LENGTH_SHORT).show();
			}
		});
	}
	//	我的领域结

}
